/**
 * @(#) DBConnection.java
 *
 * @author lihg
 * @version V1.0 2013-11-11
 *
 */

package cn.com.dshome.database.jdbc;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 
 * Purpose:数据库连接类
 * 
 * @author lihg
 * @see
 * @since
 */
@Component("dbConnection")
public class DBConnection {
	private static Logger log = Logger.getLogger(DBConnection.class) ;
	@Value("jdbc.url")
	private String url ;
	@Value("jdbc.driver")
	private String driver ;
	@Value("jdbc.username")
	private String user ;
	@Value("jdbc.password")
	private String password ;
	private Properties dbProperties ;

	public DBConnection() {
		if(null == url) {//读取配置失败，手动读取
			Properties properties = new Properties() ;
			BufferedInputStream bis = new BufferedInputStream(DBConnection.class.getResourceAsStream("/db/jdbc.properties"));
			//加载配置文件
			try {
				properties.load(bis);
				bis.close();
			} catch (IOException e) {
				DBConnection.log.error("读取配置文件失败",e) ;
			}
			url = properties.getProperty("jdbc.url") ;
			driver = properties.getProperty("jdbc.driver") ;
			user = properties.getProperty("jdbc.username") ;
			password = properties.getProperty("jdbc.password") ;
		}
		dbProperties = getDBProperties() ;
	}

	/**
	 * 获取JDBC配置信息
	 * @author lihg
	 * @version V1.0 2013-11-11
	 * @return
	 */
	private Properties getDBProperties() {
		if(null == dbProperties) {
			dbProperties = new Properties() ;
		}
		dbProperties.put("user", user) ;
		dbProperties.put("password",password) ;
		dbProperties.put("remarksReporting", "true") ;
		return dbProperties ;
	}

	/**
	 * 加载驱动
	 * @author lihg
	 * @version V1.0 2013-11-11
	 */
	private void registerDriver()  {
		if(DBConnection.log.isInfoEnabled()) {
			DBConnection.log.info("加载驱动:" + driver) ;
		}
		try {
			Class.forName(driver) ;
		} catch (ClassNotFoundException e) {
			DBConnection.log.error("加载驱动失败：找不到驱动类 " + driver) ;
			System.exit(0) ;
		}
	}

	/**
	 * 获取数据库连接
	 * @author lihg
	 * @version V1.0 2013-11-11
	 * @return
	 */
	public Connection  getConnection() {
		registerDriver() ;
		Connection connection = null ;
		try {
			connection = DriverManager.getConnection(url, getDBProperties()) ;
		} catch (SQLException e) {
			DBConnection.log.error("获取数据库连接失败" ,e) ;
			System.exit(0) ;
		}
		return connection ;
	}

	/**
	 * 关闭数据库连接
	 * @author lihg
	 * @version V1.0 2013-11-11
	 * @param rs
	 * @param pstmt
	 * @param conn
	 */
	public  void close(ResultSet rs, PreparedStatement pstmt,
			Connection conn) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pstmt != null) {
				pstmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			DBConnection.log.error("关闭连接失败",e);
		}
	}
}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * 
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2013-11-11上午11:14:23 lihg 创建版本
 */